//
//  File.swift
//
//
//  Created by didi on 2022/9/14.
//

import Fluent
import Molecular
import Vapor

public final class UserBuilder: MolecularModuleBuilder {
    var template: UserModuleTemplate?

    public required init() { template = nil }

    public convenience init(template: UserModuleTemplate? = nil) {
        self.init()
        self.template = template
    }

    override public func build() -> MolecularModule {
        UserModule(template: template ?? DefaultUserModuleTemplate())
    }
}

public struct UserInterface {
    private let db: Database

    public let account: UserAccountApi
    public let accountRole: UserAccountRoleApi
    public let role: UserRoleApi
    public let profile: UserProfileApi
    public let invitation: UserInvitationApi

    init(_ db: Database) {
        self.db = db

        account = .init(.init(db))
        accountRole = .init(.init(db))
        role = .init(.init(db))
        profile = .init(.init(db))
        invitation = .init(.init(db))
    }
}

public extension Request {
    var user: UserInterface { .init(db) }
}

public extension Application {
    var user: UserInterface { .init(db) }
}
